import random
import argparse

class CommandGenerator:
    def __init__(self, max_commands=30000, val_range=(-2**31, 2**31 - 1)):
        self.max_commands = max_commands
        self.val_range = val_range
        self.commands = ["push", "pop", "top", "getMin"]
    
    def generate(self, count=None):
        if count is None:
            count = random.randint(1, self.max_commands)
        
        cmds = []
        stack_size = 0
        
        for _ in range(count):
            # 确保 pop, top, getMin 只在非空栈上调用
            if stack_size == 0:
                cmd = "push"
            else:
                cmd = random.choice(self.commands)
            
            if cmd == "push":
                val = random.randint(*self.val_range)
                cmds.append((cmd, val))
                stack_size += 1
            elif cmd == "pop":
                cmds.append((cmd,))
                stack_size -= 1
            else:  # top or getMin
                cmds.append((cmd,))
        
        return cmds

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Generate test commands for MinStack")
    parser.add_argument('--count', type=int, default=100, help='Number of commands to generate')
    parser.add_argument('--output', type=str, default='test_commands.txt', help='Output file name')
    args = parser.parse_args()
    
    generator = CommandGenerator()
    commands = generator.generate(args.count)
    
    with open(args.output, 'w') as f:
        for cmd in commands:
            f.write(f"{cmd[0]}" + (f" {cmd[1]}\n" if len(cmd) > 1 else "\n"))
    
    print(f"Generated {args.count} test commands to {args.output}")    